package com.wq.biz;

import java.awt.Color;
import java.awt.Component;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.wq.ui.coms.FontCreater;
import com.wq.ui.coms.ImageGet;

/**
 * 主要控制类
 * @author wq
 * 2020/12/19 - 17:43
 *
 */
public class Manager {
	private static Manager m = new Manager();
	private Manager()
	{
		
	}
	public static Manager getManager()
	{
		return m;
	}
	
	private int nowDice = -1;
	private int playerNum = 4;
	private Dice dice ;
	private Role[] roles;
	private int turn = 0;
	private JPanel gamePanel;
	private JLabel b = 	new JLabel();
	
	String[] color = new String[]{"橙色","绿色","红色","蓝色"};
	
	
	//初始化棋盘
	
	public void iniChessPane(JPanel gamePanel)
	{
		this.gamePanel = gamePanel;
	}
	
	//开始游戏
	public void start(int Num)
	{
		if(Num > 0){
			playerNum = Num;
		}
		ChessPane.getChessPane().iniChess(playerNum);
		dice = new Dice(ImageGet.getImageGet().getDice());
		
		Component comp[] = gamePanel.getComponents();
		for(int c = 0 ; c< comp.length ;c++) //返回后清空组件
		{
			if(comp[c] instanceof Plane || comp[c] instanceof Dice)
			{
				gamePanel.remove(comp[c]);
			}
		}
		
		
		
		roles = new Role[playerNum];
		for(int c = 0 ; c < playerNum ; c++) 
		{
			roles[c] = new Role(c);
			Plane[] tempP = roles[c].getPlane();
			for(int c1 = 0 ; c1< tempP.length ;c1++)
			{
				tempP[c1].setSize(35,35);
				gamePanel.add(tempP[c1]);
			}
			ChessPane.getChessPane().iniPlanes(tempP);
			
		}
		dice.setCanRoll(true);
		dice.setBounds(875, 375, 50, 50);
		gamePanel.add(dice);
		tips(turn);
	}
	
	
	//显示点数
	public void hasThrowDice(int diceNum)
	{
		this.nowDice = diceNum;
		dice.setCanRoll(false);
		setCanMove();
	}
	//判断可使用飞机
	private void setCanMove()
	{
		for(int c = 0 ;  c< roles.length ;c++)
		{
			roles[c].setNotYourTurn();
		}
		Plane[] tempP= roles[turn].getPlane();
		int falseNum = 0;
		for(int c = 0 ; c < tempP.length ; c++)
		{
			int len = tempP[c].getLen();
			if( len >ChessPane.INI_LEN || nowDice == 6)
			{
				if(len<ChessPane.OVER_LEN)
				{
					tempP[c].setCanMove(true);
				}else
				{
					tempP[c].setCanMove(false);
				}
			}else
			{
				tempP[c].setCanMove(false);
				falseNum++;
			}
		}
		if(falseNum == tempP.length)
		{
			nextRole(nowDice == 6);
		}	
		
	}
	
	//控制飞机移动
	public void hasClickPlane(Plane thePlane) //
	{
		gamePanel.setComponentZOrder(thePlane, 0);
		
		Plane[] ps = roles[thePlane.getMaster()].getPlane();
		for(int c = 0 ;c < ps.length ; c ++)
		{
			ps[c].setCanMove(false);
		}
		//增加正常位置
		Point[] p = ChessPane.getChessPane().getNextPoint(thePlane.getMaster(), thePlane.getIndex(),thePlane.getLen(), nowDice);
		thePlane.setNextPosition(p);
		if(thePlane.getLen() == -1)
		{
			thePlane.addLen(1);
		}else
		{
			if(thePlane.getLen() > 50)
			{
				if(thePlane.getLen() + nowDice > ChessPane.OVER_LEN)
				{
					int minor = ChessPane.OVER_LEN - (thePlane.getLen() +  nowDice );
					int add = nowDice + minor;
					thePlane.addLen(add);
					thePlane.addLen(minor);
				}else
				{
					thePlane.addLen(nowDice);
				}
			}else
			{
				thePlane.addLen(nowDice);
			}
		}
		
		Plane likelyPlane = judgeCollision(thePlane);
		if(likelyPlane == null)
		{
			if(judgeSpecial(thePlane))
			{
				//加距离
				Point[] specialP = ChessPane.getChessPane().getNextPoint(thePlane.getMaster(), thePlane.getIndex(), thePlane.getLen(), ChessPane.SPECIAL_LEN);
				thePlane.addLen(ChessPane.SPECIAL_LEN);
				thePlane.setNextPosition(specialP);
				Plane likelyPlane2 = judgeCollision(thePlane);
				if(likelyPlane2 != null)
				{
					likelyPlane2.reset(thePlane);	
					nextRole(nowDice == 6);
				}
				if(judgeSkip(thePlane))
				{
					//加距离
					Point[] skipP = ChessPane.getChessPane().getNextPoint(thePlane.getMaster(),thePlane.getIndex(),  thePlane.getLen(), ChessPane.SKIP_LEN);
					thePlane.addLen(ChessPane.SKIP_LEN);
					thePlane.setNextPosition(skipP);
					Plane likelyPlane3 = judgeCollision(thePlane);
					if(likelyPlane3 != null)
					{
						likelyPlane3.reset(thePlane);	
						nextRole(nowDice == 6);
					}
					
					if(judgeSpecial(thePlane))
					{
						//加距离
						Point[] specialP1 = ChessPane.getChessPane().getNextPoint(thePlane.getMaster(),thePlane.getIndex(),  thePlane.getLen(), ChessPane.SPECIAL_LEN);
						thePlane.addLen(ChessPane.SPECIAL_LEN);
						thePlane.setNextPosition(specialP1);
						Plane likelyPlane21 = judgeCollision(thePlane);
						if(likelyPlane21 != null)
						{
							likelyPlane21.reset(thePlane);	
							nextRole(nowDice == 6);
							return;
						}
					}
				}
			}
			if(thePlane.getLen() == ChessPane.OVER_LEN){
				thePlane.isOver();
//				thePlane.setIcon(/);
				roles[thePlane.getMaster()].isOver ++;
			}
			else
			{
				if(judgeSkip(thePlane))
				{
					//加距离
					Point[] skipP = ChessPane.getChessPane().getNextPoint(thePlane.getMaster(), thePlane.getIndex(), thePlane.getLen(), ChessPane.SKIP_LEN);
					thePlane.addLen(ChessPane.SKIP_LEN);
					thePlane.setNextPosition(skipP);
					Plane likelyPlane3 = judgeCollision(thePlane);
					if(likelyPlane3 != null)
					{
						likelyPlane3.reset(thePlane);	
						nextRole(nowDice == 6);
					}
					if(judgeSpecial(thePlane))
					{
						//加距离
						Point[] specialP = ChessPane.getChessPane().getNextPoint(thePlane.getMaster(),thePlane.getIndex(),  thePlane.getLen(), ChessPane.SPECIAL_LEN);
						thePlane.addLen(ChessPane.SPECIAL_LEN);
						thePlane.setNextPosition(specialP);
						Plane likelyPlane2 = judgeCollision(thePlane);
						if(likelyPlane2 != null)
						{
							likelyPlane2.reset(thePlane);	
							nextRole(nowDice == 6);
							return;
						}
					}
				}
			}
		}
		else
		{
			
			likelyPlane.reset(thePlane);
			
		}
		
		nextRole(nowDice == 6);
	}
	
	
	//判断是否碰撞
	
	private Plane judgeCollision(Plane thePlane) 
	{
		for(int c = 0 ;c < roles.length ;c++)
		{
			Plane[] planes = roles[c].getPlane();
			for(int c1 = 0 ;c1 < planes.length ; c1++)
			{
				int aLen = ChessPane.getChessPane().getAbsoluteLen(planes[c1].getMaster(), planes[c1].getLen());
				int bLen = ChessPane.getChessPane().getAbsoluteLen(thePlane.getMaster(), thePlane.getLen());
				if(aLen > 0)
				{
					System.out.println("thePlane " + bLen + "  aLen " + aLen);
				}
				
				if(aLen == bLen && thePlane.getMaster() != planes[c1].getMaster() && aLen >= 0 && aLen <= 50 )
				{//CHANGE
					thePlane.setLocation(thePlane.getX()-4, thePlane.getY());
					return planes[c1];
				}
			}
		}
		return null;
	}
	
	
	//判断特殊跳跃
	
	private boolean judgeSpecial(Plane thePlane)
	{
//		System.out.println("judge"+ChessPane.getChessPane().isSpecial(thePlane.getLen()));
		return ChessPane.getChessPane().isSpecial(thePlane.getLen());
	}
	
	
	//判断普通跳跃
	
	private boolean judgeSkip(Plane thePlane) 
	{
		return ChessPane.getChessPane().isSkip(thePlane.getLen());
	}
	
	
	//轮到下一个玩家
	
	private void nextRole(boolean reTurn)
	{
		if(!reTurn)
		{
			turn++;
			if(turn > roles.length-1)
			{
				turn = 0;
			}
//			System.out.println(nowRollIsOver());
			if(nowRollIsOver())
			{
				nextRole(false);
			}else
			{
				dice.setCanRoll(true);
			}
			tips(turn);
			
		}else
		{
			
			dice.setCanRoll(true);
			
		}
	}
	
	
	//判断是否再掷一次
	
	private boolean nowRollIsOver()
	{
		Plane[] tempP = roles[turn].getPlane();
		int overNum = 0;
		for(int c = 0 ; c < tempP.length ; c++)
		{
			if(tempP[c].getLen() == ChessPane.OVER_LEN)
			{
				overNum++;
			}
		}
		return overNum == tempP.length;
	}
	
	
	//得到当前点数
	
	public int getNowDice() { 
		return nowDice;
	}
	
	
	//判断游戏是否结束
	
	public int judgeOver(){//CHANGE
		for(int i=0;i < roles.length;i++){
			if( roles[i].isOver == 4){
				return i;
			}
			
		}
		return -1;
	}
	
	
	//显示掷骰子提示
	
	public void tips(int thisTurn){ //CHANGE
		if(judgeOver()==-1){
		gamePanel.add(b);
		b.setBounds(800, 290, 170, 50);
		b.setForeground(new Color(154, 86, 42).darker());
		b.setText("现在由"+color[thisTurn] + "玩家掷骰子");
		b.setFont(FontCreater.getDefalut().deriveFont(15f));
		}
		else{
			b.setText(color[judgeOver()]+"获得胜利！");
		}
		
	}
	
}
